﻿/* 
 * author: www.cnblogs.com/luminji
 * date  : 2011/12/28
 */
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Luminji.Infrastructure.UnitOfWork;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Luminji.Core.Enterprise.RBAC;
using Luminji.Exam.Model.Users;

namespace Luminji.Exam.Model.Papers
{

    public class PaperService
    {
        IPaperRepository _paperRepository;
        IUnitOfWork _unitOfWork;

        public PaperService(IPaperRepository paperRepository, IUnitOfWork unitOfWork)
        {
            _paperRepository = paperRepository;
            _unitOfWork = unitOfWork;
        }

        public Paper GetPaperWithDetailDetailByPaperCode(string papercode)
        {
            var paper = _paperRepository.FindWithDetailByPaperCode(papercode);
            PaperFormator.PaperDetailsFormat(paper.PaperDetails, ConfigurationManager.AppSettings["ContentFormat"]);
            return paper;
        }

        public int GetPapersNumsBySubjectAndChildrenCode(string code)
        {
            return _paperRepository.FindPapersNumsBySubjectAndChildrenCode(code);
        }

        public IList<Paper> GetPapersBySubjectAndChildrenCodeInPage(string code, int pageNum)
        {
            return _paperRepository.FindBySubjectAndChildrenCodeInPage(code, pageNum);
        }

        public IList<Paper> GetPapers(int? showType, IUser currentUser)
        {
            if (showType == null || currentUser == null)
            {
                return null;
            }
            var user = currentUser as ExamUser;
            if (user.IsGuest())
            {
                return null;
            }
            //get all papers, not paged
            return _paperRepository.Find(user.UserCode, showType.Value);
        }
    }
}
